package cn.js.sangu.unitStore.service;

import cn.js.sangu.unitStore.common.exception.ServiceException;
import cn.js.sangu.unitStore.entity.Notice;
import cn.js.sangu.unitStore.mapper.NoticeMapper;
import cn.js.sangu.unitStore.utils.LinsUtil;
import cn.js.sangu.unitStore.utils.TokenUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 说明：公告服务
 * 作者：linshuo
 * 日期：2024/8/7 9:14
 */
@Slf4j
@Service
public class NoticeService extends ServiceImpl<NoticeMapper, Notice> {

//    @Resource
//    NoticeMapper noticeMapper;
    
    public Notice saveInfo(Notice dto){
        dto.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        this.keywordReplace(dto);
        if (!super.save(dto)){
            throw new ServiceException("保存失败！请联系管理员");
        }
        return dto;
    }

    public Notice updateInfo(Notice dto){
        dto.setModifier(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        this.keywordReplace(dto);
        if (!super.updateById(dto)){
            throw new ServiceException("更新失败！请联系管理员");
        }
        return dto;
    }

    private void keywordReplace(Notice dto){
        dto.setTitle(LinsUtil.keywordReplace(dto.getTitle(), null, null));
        dto.setContent(LinsUtil.keywordReplace(dto.getContent(), null, null));
    }

}
